﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Guline.SupportCMS.Models.Object;
using Guline.SupportCMS.Models.Context;

namespace Guline.SupportCMS.Controllers.Manage
{
    public class MenuItemController : GulineBaseController
    {

        public ActionResult jsonList(string MenuID, string ParentID)
        {
            int pParentID = 0;
            int pMenuID = 1;
            try
            {
                pMenuID = int.Parse(MenuID);
            }
            catch { }
            try
            {
                pParentID = int.Parse(ParentID);
            }
            catch { }

            if (pParentID == 0)
            {
                var menuitem = from m in db.MenuItems where m.MenuID == pMenuID && m.ParentID == null select new { m.Enable, m.ID, m.Name, m.Menu_URL, ParentName = m.Parent.Name };
                return Json(menuitem, JsonRequestBehavior.AllowGet);
            }
            else
            {
                var menuitem = from m in db.MenuItems where m.MenuID == pMenuID && m.ParentID == pParentID select new { m.Enable, m.ID, m.Name, m.Menu_URL, ParentName = m.Parent.Name };
                return Json(menuitem, JsonRequestBehavior.AllowGet);
            }
        }
        //
        // GET: /MenuItem/

        public ActionResult Index()
        {
            if (!string.IsNullOrWhiteSpace(Request.QueryString["ParentID"]))
            { 
                int id = int.Parse(Request.QueryString["ParentID"]);
                var menuitem = db.MenuItems.Where(m => m.ID == id).Include(m => m.Parent).FirstOrDefault();
                ViewBag.MenuItem = menuitem;
            }
            return View();
        }

        //
        // GET: /MenuItem/Details/5

        public ActionResult Details(int id = 0)
        {
            MenuItem menuitem = db.MenuItems.Find(id);
            if (menuitem == null)
            {
                return HttpNotFound();
            }
            return View(menuitem);
        }

        //
        // GET: /MenuItem/Create

        public ActionResult Create()
        {
            ViewBag.ParentID = new SelectList(db.MenuItems.Where(m => m.ParentID == null), "ID", "Name");
            ViewBag.ColorStyleID = new SelectList(db.ColorStyles, "ID", "Name");
            ViewBag.MenuID = new SelectList(db.Menus, "ID", "Name");
            ViewBag.Categorys = db.Categories.Where(m => m.Enable && m.ParentID == null).Include(m => m.Childrens).OrderBy(m => m.ParentID).ToList();
            return View();
        }

        //
        // POST: /MenuItem/Create

        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Create(MenuItem menuitem)
        {
            if (ModelState.IsValid)
            {
                db.MenuItems.Add(menuitem);
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            ViewBag.Categorys = db.Categories.Where(m => m.Enable && m.ParentID == null).Include(m => m.Childrens).OrderBy(m => m.ParentID).ToList();

            ViewBag.ParentID = new SelectList(db.MenuItems.Where(m => m.ParentID == null), "ID", "Name", menuitem.ParentID);
            ViewBag.ColorStyleID = new SelectList(db.ColorStyles, "ID", "Name", menuitem.ColorStyleID);
            ViewBag.MenuID = new SelectList(db.Menus, "ID", "Name", menuitem.MenuID);
            return View(menuitem);
        }

        //
        // GET: /MenuItem/Edit/5

        public ActionResult Edit(int id = 0)
        {
            MenuItem menuitem = db.MenuItems.Find(id);
            if (menuitem == null)
            {
                return HttpNotFound();
            }
            ViewBag.Categorys = db.Categories.Where(m => m.Enable && m.ParentID == null).Include(m => m.Childrens).OrderBy(m => m.ParentID).ToList();
            ViewBag.ParentID = new SelectList(db.MenuItems.Where(m => m.ParentID == null), "ID", "Name", menuitem.ParentID);
            ViewBag.ColorStyleID = new SelectList(db.ColorStyles, "ID", "Name", menuitem.ColorStyleID);
            ViewBag.MenuID = new SelectList(db.Menus, "ID", "Name", menuitem.MenuID);
            return View(menuitem);
        }

        //
        // POST: /MenuItem/Edit/5

        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Edit(MenuItem menuitem)
        {
            if (ModelState.IsValid)
            {
                db.Entry(menuitem).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            ViewBag.Categorys = db.Categories.Where(m => m.Enable && m.ParentID == null).Include(m => m.Childrens).OrderBy(m => m.ParentID).ToList();
            ViewBag.ParentID = new SelectList(db.MenuItems.Where(m => m.ParentID == null), "ID", "Name", menuitem.ParentID);
            ViewBag.ColorStyleID = new SelectList(db.ColorStyles, "ID", "Name", menuitem.ColorStyleID);
            ViewBag.MenuID = new SelectList(db.Menus, "ID", "Name", menuitem.MenuID);
            return View(menuitem);
        }

        //
        // GET: /MenuItem/Delete/5

        public ActionResult Delete(int id = 0)
        {
            MenuItem menuitem = db.MenuItems.Find(id);
            if (menuitem == null)
            {
                return HttpNotFound();
            }
            return View(menuitem);
        }

        //
        // POST: /MenuItem/Delete/5

        [HttpPost, ActionName("Delete")]
        [ValidateAntiForgeryToken]
        public ActionResult DeleteConfirmed(int id)
        {
            MenuItem menuitem = db.MenuItems.Find(id);
            db.MenuItems.Remove(menuitem);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}